package cn.neusoft.controller;

import cn.neusoft.po.Category;
import cn.neusoft.po.Product;
import cn.neusoft.po.ProductDetail;
import cn.neusoft.query.ProductQuery;
import cn.neusoft.service.ICategoryService;
import cn.neusoft.service.IProductDetailService;
import cn.neusoft.service.IProductService;
import cn.neusoft.vo.ResponseObj;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pro")
public class ProductController {

    @Autowired
    ICategoryService service;

    @GetMapping("/catTree")
    public ResponseObj tree(){
        List<Category> tree = service.tree();
        return ResponseObj.SUCCESS(tree);
    }

    @Autowired
    IProductService productService;

    @PostMapping("/page")
    public ResponseObj getByPage(@RequestBody ProductQuery query){
        IPage<Product> page = new Page(query.getCurrent(),query.getSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(query.getCatId()!=null,"cat_id",query.getCatId());
        queryWrapper.like(StringUtils.hasText(query.getName()),"name",query.getName());
        //构建条件搜索对象

        IPage<Product> iPage = productService.page(page, queryWrapper);
        return ResponseObj.SUCCESS(iPage);
    }

    @GetMapping("/{id}")
    public ResponseObj getById(@PathVariable("id") Integer id){
        Product product = productService.getById(id);
        return ResponseObj.SUCCESS(product);
    }

    @Autowired
    IProductDetailService productDetailService;

    @GetMapping("/detail/{id}")
    public ResponseObj getDetail(@PathVariable("id") Integer pid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("product_id",pid);
        ProductDetail productDetail = productDetailService.getOne(queryWrapper);
        return ResponseObj.SUCCESS(productDetail);
    }


}
